package advanced.concurrent;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerTest {
    private static int n = 0;
    private static AtomicInteger n2 = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        int times = 100000;
        int times2 = 5000000;
        ExecutorService service = Executors.newFixedThreadPool(10);
        //i++引发的线程问题
        Thread t1 = new Thread() {
            public void run() {
                for (int i = 0; i < times; i++) {
                    n++;
                    n2.decrementAndGet();
                }
            }

            ;
        };
        Thread t2 = new Thread() {
            public void run() {
                for (int i = 0; i < times2; i++) {
                    n++;
                }
            }

            ;
        };
        service.execute(t1);
        service.execute(t2);
//        t1.join();
//        t2.join();
        service.awaitTermination(1, TimeUnit.SECONDS);
        service.shutdown();
        System.out.println("最终n的值为：" + n);
        System.out.println(n2);

    }
}
